home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / AIAT / Examples / Sources / DemoAbstractor.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-16  |  6.0 KB  |  290 lines  |  [TEXT/CWIE]

  1. /// DemoAbstractor.cpp
  2. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  3.  
  4.  
  5. #include "Abstractor.h"
  6. #include "IAExtentCorpus.h"
  7. #include "EnglishAnalysis.h"
  8.  
  9. #ifdef __mac_os
  10. #include <Files.h>
  11. #include <Types.h>
  12. #include "HFSStorage.h"
  13. #else
  14. #include <sys/stat.h>
  15. #include "UFSStorage.h"
  16. #endif
  17.  
  18.  
  19. #ifdef DEBUG_NEW
  20. #include <DebugNew.h>
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26.  
  27. #if __profile__
  28. #include <profiler.h>
  29. #endif
  30.  
  31. #ifndef __mac_os
  32. typedef char* StringPtr;
  33. #endif
  34.  
  35. void DumpInformation(const IADocumentAbstractor& abstractor, uint32 showlevels = 1);
  36.  
  37. void DemoAbstractor (StringPtr file);
  38. void DemoAbstractor (StringPtr file) {
  39. #ifdef __mac_os
  40.     FSSpec            mMacFileSpec;
  41.     short            mDataForkRefNum;
  42. #else 
  43.     FILE*            mFileSpec;
  44.     struct    stat    fileStat;
  45. #endif
  46.     
  47.     char* stopwordFile = "EnglishStopwords";
  48.     char* stemDictDoc = "EnglishSubstitutions";
  49.     char* abbrevFile = "EnglishAbbreviations";
  50. #ifdef __mac_os
  51.     IAStorage* storage = MakeHFSStorage(0,0,"\ptemp.index");
  52. #else
  53.     IAStorage* storage = MakeFileStorage("temp.index");
  54. #endif
  55.  
  56.     IADeleteOnUnwind delStorage(storage);
  57.     EnglishAnalysis* ana = new EnglishAnalysis(stopwordFile, stemDictDoc);
  58.     
  59.             
  60.         uint32 numberOfSentences = 1;
  61.         TermIndex* contextIndex = NULL;
  62.         clock_t progFrequency = 10000;
  63.         void* callerData = NULL;
  64.         RankedProgressFn* progfn = NULL;        
  65.  
  66. #ifdef __mac_os        
  67.         OSErr iErr = FSMakeFSSpec(    0, 0, file, &mMacFileSpec);
  68.     
  69. // open file    
  70.         OSErr    err = FSpOpenDF(&mMacFileSpec, fsRdPerm, &mDataForkRefNum);  
  71.         if (err != noErr) {
  72.             return;
  73.         }
  74.  
  75. // reads the file
  76.         Handle    dataHandle = nil;
  77.     
  78.         long    fileLength;
  79.         err = GetEOF(mDataForkRefNum, &fileLength);
  80.         if (err != noErr) {
  81.             return;
  82.         }
  83.         dataHandle = NewHandle(fileLength + 1);
  84.         if (dataHandle == nil) return;
  85.         
  86.         err = SetFPos(mDataForkRefNum, fsFromStart, 0);
  87.         if (err != noErr) {
  88.             return;
  89.         }
  90.         HLock(dataHandle);    
  91.     
  92.         err = FSRead(mDataForkRefNum, &fileLength, *dataHandle);
  93.         if (err != noErr) {
  94.             return;
  95.         }
  96.     
  97.         *((*dataHandle)+fileLength) = '\0';
  98.         char* buffer = (char*)(*dataHandle); 
  99. #else
  100.         mFileSpec = fopen(file, "r");
  101.         if (mFileSpec == NULL) {
  102.             return;
  103.         }
  104.     
  105.     long    fileLength;
  106. #ifndef WIN32
  107.     lstat(file, &fileStat);
  108. #else
  109.     stat(file, &fileStat);
  110. #endif
  111.     
  112.     fileLength = fileStat.st_size;
  113.     char* buffer = (char*)malloc (fileLength + 1);
  114.     buffer[fileLength] = '\0';
  115.  
  116.     fread (buffer, fileLength, 1, mFileSpec);
  117. #endif
  118.         uint32 bufferLength = strlen(buffer); // length of document buffer;
  119.         
  120.         IAExtentParser* parser = new IAANSISentenceParser((byte*)buffer, bufferLength, abbrevFile); // NEW INTERFACE
  121.         IADocumentAbstractor abstractor(parser, storage, ana);  // NEW INTERFACE
  122.         abstractor.Summarize(progfn, progFrequency, callerData, numberOfSentences,    contextIndex);
  123.                             
  124.         //
  125.         // Using the GetNumberOfSentences and GetSentences functions below
  126.         // you can loop and get the top ranked sentence;
  127.         //
  128.             uint32 showthismanysentences = 1;                
  129.             DumpInformation(abstractor, showthismanysentences);    
  130.  
  131.     delete parser;
  132.  
  133. #ifdef __mac_os
  134.     HUnlock (dataHandle);
  135.  
  136. // close the file
  137.         
  138.     err = FSClose(mDataForkRefNum);
  139.         
  140.     if (err != noErr) {
  141.         return;
  142.     }
  143.     FlushVol(nil, mMacFileSpec.vRefNum);
  144. #else
  145.     fclose(mFileSpec);
  146.     free (buffer);
  147. #endif
  148. }
  149.  
  150. void DumpInformation(const IADocumentAbstractor& abstractor, uint32 showlevel)
  151. {
  152.     
  153.     uint32 paragraphNumber = 0;
  154.     bool firstHasBeenShown = false;
  155.     bool showScore = true;
  156.     bool showRank = true;
  157.     uint32 numberTopWords = 5;
  158.     bool showSentences = true;
  159.     
  160.     uint32 cnt =          abstractor.GetNumberOfExtents();
  161.     IAExtentDoc** sentences = (IAExtentDoc**)abstractor.GetExtents();
  162.  
  163.     for(int i=0; i< cnt; i++) {            
  164.         if(sentences[i] && (sentences[i]->GetRank() < showlevel) && (sentences[i]->GetLength() > 0)) {
  165.             IAExtentDoc* doc = sentences[i];
  166.             
  167.             if(!firstHasBeenShown){
  168.                 firstHasBeenShown = true;
  169.                 paragraphNumber =  doc->GetGroupNumber();
  170.             }
  171.                 
  172.             if( doc->GetGroupNumber() > paragraphNumber){
  173.                 paragraphNumber = doc->GetGroupNumber();
  174.                 printf ("Paragraph Seperator\r\r", 2);
  175.             }
  176.  
  177.             if(showScore){
  178.                 printf("(%3.2f) ", doc->GetRankedHit()->GetScore());
  179.             }
  180.             if(showRank){
  181.                 printf("(%d)    ", 1 + (int)doc->GetRank());
  182.             }
  183.             if(numberTopWords >0 ) {
  184.                 if(showlevel > 0)    {
  185.                     printf ("[");
  186.                 }
  187.                 
  188.                 uint32  numberToShow = numberTopWords;
  189.                 if (numberToShow > doc->GetRankedHit()->GetMatchingTermsLen()) {
  190.                     numberToShow = doc->GetRankedHit()->GetMatchingTermsLen();
  191.                 }
  192.     
  193.                 for(int w = 0 ; w< numberToShow; w++) {
  194.                     printf ("%s ", doc->GetRankedHit()->GetMatchingTerms()[w]->GetData());
  195.                 }
  196.                 if(showlevel >0) {
  197.                     printf ("]\n");
  198.                 }
  199.             }
  200.             if(showSentences) {
  201. #if 0
  202.                 int l = doc->GetLength();
  203.                 char* t1 = new char[l+1];
  204.                 memcpy(t1, (char*)doc->GetText() + doc->GetOffset(), l);
  205.                 t1[l] = '\0';
  206.                 printf ("Summary => %s\n", t1);
  207.                 delete [] t1;
  208. #endif
  209.                 uint32 l = doc->GetLength();;
  210.                 char* t1 = (char*)doc->GetExtent();
  211.                 printf ("Summary => %s\n", t1);
  212.                 IAFreeArray(t1);
  213.             }
  214.         }
  215.     }
  216.     
  217. }
  218.  
  219. StringPtr GetFolderPath();
  220. StringPtr GetFolderPath()
  221. {
  222.  
  223.   char lpath[1024];
  224.   int i = 0;
  225.   printf ("Enter File Path >> ");
  226.   while (!feof(stdin)) {
  227.     char cc = fgetc(stdin);
  228.     if (cc == '\n') break;
  229.     lpath[i++] = cc;
  230.   }
  231.   lpath[i] = '\0';
  232.   if (i == 0) return NULL;
  233.   StringPtr folder = (StringPtr)IAMallocArraySized(byte, i + 1);
  234. #ifdef __mac_os
  235.   folder[0] = i;
  236.   memcpy(folder + 1, lpath, i);
  237. #else
  238.   memcpy(folder, lpath, i);
  239.   folder[i] = '\0';
  240. #endif
  241.   return folder;
  242. }
  243.  
  244. void main() {
  245.  
  246. #if __profile__
  247. #ifdef powerc
  248.     ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
  249. #else
  250.     ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
  251. #endif    
  252. #endif
  253.  
  254.     StringPtr file = GetFolderPath();
  255.  
  256.     IATry {
  257.         if (file)
  258.             DemoAbstractor(file);  
  259.     }
  260.     IACatch (const IAException& exception) {
  261.         printf("Caught exception: \n", exception.What());
  262.     }
  263.     
  264.     if (file != NULL) {
  265. #ifdef __mac_os
  266.     IAFreeArraySized (file, byte, file[0] + 1);
  267. #else
  268.     IAFreeArraySized (file, byte, strlen(file) + 1);
  269. #endif    
  270.     }
  271.  
  272.  
  273. #if __profile__
  274.     ProfilerDump("\pDemoAccessor.prof");
  275.     ProfilerTerm();
  276. #endif
  277.  
  278. #ifdef IADEBUG
  279.     IAReportMemoryUsage();
  280. #endif
  281.  
  282. #ifdef DEBUG_NEW
  283.     DebugNewReportLeaks();
  284. #endif
  285. }
  286.  
  287.  
  288.  
  289.  
  290.